//
//  CZAddAddressViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/7.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZAddAddressViewController.h"
#import "CZAddAddressCell.h"
static NSString * const add_address_cell = @"add_address_cell";

@interface CZAddAddressViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic, strong) UITableView *addAddressTableView;

@end

@implementation CZAddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"新增收货地址";
    UIButton *rightBarBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    rightBarBtn.frame = CGRectMake(0, 0, 54, 25);
    [rightBarBtn addTarget:self action:@selector(saveAddress) forControlEvents:(UIControlEventTouchUpInside)];
    rightBarBtn.backgroundColor = mRGBColor(214, 221, 220);
    [rightBarBtn setTitle:@"保存" forState:(UIControlStateNormal)];
    rightBarBtn.layer.cornerRadius = 2;
    rightBarBtn.clipsToBounds = YES;
    rightBarBtn.tintColor = ThemeColor;
    rightBarBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];
    self.view.backgroundColor = GLOBAL_BACKGROUND_COLOR;
    UITableView *addAddressTableView = [[UITableView alloc] init];
    [self.view addSubview:addAddressTableView];
    [addAddressTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.bottom.equalTo(self.view);
    }];
    addAddressTableView.backgroundColor = GLOBAL_BACKGROUND_COLOR;
    addAddressTableView.delegate = self;
    addAddressTableView.dataSource = self;
    addAddressTableView.estimatedRowHeight = 75;
    addAddressTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [addAddressTableView registerNib:[UINib nibWithNibName:@"CZAddAddressCell" bundle:nil] forCellReuseIdentifier:add_address_cell];
    _addAddressTableView = addAddressTableView;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 20)];
    headerView.backgroundColor = GLOBAL_BACKGROUND_COLOR;
    addAddressTableView.tableHeaderView = headerView;
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 50)];
    UIButton *defaultBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    defaultBtn.frame = CGRectMake(20, 0, 30, 30);
    defaultBtn.centerY = footerView.centerY;
    UILabel *defaultLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    defaultLabel.left = defaultBtn.right + 20;
    defaultLabel.centerY = defaultBtn.centerY;
    defaultLabel.text = @"默认";
    defaultLabel.textColor = GLOBAL_TEXT_COLOR;
    [footerView addSubview:defaultBtn];
    [footerView addSubview:defaultLabel];
    addAddressTableView.tableFooterView = footerView;
    // Do any additional setup after loading the view.
}
- (void)saveAddress {
    [self.view endEditing:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CZAddAddressCell *cell = (CZAddAddressCell *)[tableView dequeueReusableCellWithIdentifier:add_address_cell];
    cell.addressTextField.delegate = self;
    cell.backgroundColor = GLOBAL_BACKGROUND_COLOR;
    if (!(indexPath.row > 1 && indexPath.row < 5)) {
        cell.dropDownBtn.hidden = YES;
    }
    return cell;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    CZAddAddressCell *cell = (CZAddAddressCell *)textField.superview.superview;
    NSIndexPath *indexPath = [_addAddressTableView indexPathForCell:cell];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
